//
//  EditPersonController.m
//  iStage
//
//  Created by michael on 27/07/2008.
//  Copyright 2008 Legal Education Technology Lab. All rights reserved.
//

#import "EditPersonController.h"


@implementation EditPersonController
@synthesize personPicker,textEditor, ViewType;

- (id)initWithStyle:(UITableViewStyle)style {
	if (self = [super initWithStyle:style]) {
	}
	return self;
}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
	return 1;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
	return 2;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
	
	static NSString *MyIdentifier = @"MyIdentifier";
	
	UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
	if (cell == nil) {
		cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:MyIdentifier] autorelease];
	}
	// Configure the cell
//	dataSource = [[DataManager sharedDataManager].currentProduction objectForKey:@"crewlist"];
	NSMutableDictionary *personInfo = [DataManager sharedDataManager].currentPerson;
	switch (indexPath.row) {
		case 0:	/* role */
			cell.text=[personInfo objectForKey:@"role"];
			break;
		case 1:
		{
			NSLog(@"Calculating Performer display");
			ABAddressBookRef addressbook = ABAddressBookCreate();
			NSString *displayText =@"unallocated";
			NSString *actorName =@"Not Cast";
			BOOL cast =NO;
			//displayText= [personInfo objectForKey:@"role"];
			//NSString *charName = [dataSource objectForKey:@"role"];
			NSNumber *o =[personInfo objectForKey:@"actorabref"];
			//				int actorid = [[NSNumber numberWithInteger:3] intValue];
			//				[personInfo objectForKey:@"actorabref"];
			NSLog([[NSString alloc] initWithFormat:@"Stored Actorref ID: %u", o]);
			ABRecordRef actor = ABAddressBookGetPersonWithRecordID(addressbook, [o intValue]);
			
			if (actor == nil|[o intValue] ==0) {
				actorName = @"Not Cast";
			}
			else  {
				actorName = [[NSString alloc] initWithFormat:@"%@ %@", ABRecordCopyValue(actor, kABPersonFirstNameProperty),ABRecordCopyValue(actor, kABPersonLastNameProperty)];
				cast =YES;
			}
			//displayText = actorName;//[[NSString alloc] initWithFormat:@"%@ - %@",displayText,actorName];
			cell.text=actorName;
			if (cast )
			{
				cell.accessoryType = UITableViewCellAccessoryCheckmark;
			}
			else {
				cell.accessoryType = UITableViewCellAccessoryNone;
			}
			CFRelease(addressbook);
		}
		default:
			break;
	}
	return cell;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
	switch (indexPath.row) {
		case 0:
			NSLog(@"Editing Role");

			textEditor.valueName =@"role";
			textEditor.value=@"guff";
			
			[self presentModalViewController:textEditor animated:YES];
			break;
		case 1:
			
			[self presentModalViewController:personPicker animated:YES];
		default:
			break;
	}
}

-(NSString*)tableView:(UITableView*)tableView titleForHeaderInSection:(NSInteger)section{
	if (section==0) {
		if (ViewType==0) {
			return @"Casting Information";
		}
		if (ViewType==1) {
			return @"Crew Information";
		}
	}
	return @"Not Set!bad";
}
/*
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
	
	if (editingStyle == UITableViewCellEditingStyleDelete) {
	}
	if (editingStyle == UITableViewCellEditingStyleInsert) {
	}
}
*/
/*
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
	return YES;
}
*/
/*
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/
/*
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
	return YES;
}
*/


- (void)dealloc {
	[personPicker release];
	[super dealloc];
	
}


- (void)viewDidLoad {
	[super viewDidLoad];
	textEditor = [[EditPersonRoleController alloc] initWithNibName:@"EditStringView" bundle:nil];
	self.navigationItem.leftBarButtonItem = 
	[[UIBarButtonItem alloc]  initWithTitle:@"Back" style:UIBarButtonItemStyleBordered target:self action:@selector(backToPeopleList)];
	personPicker =[[ABPeoplePickerNavigationController alloc]init];
	personPicker.peoplePickerDelegate=self;
}


- (void)viewWillAppear:(BOOL)animated {
	[super viewWillAppear:animated];
	if (ViewType==0) {
		self.title=@"Edit Cast";
	}
	if (ViewType ==1) {
		self.title=@"Edit Crew";
	}
	NSLog(@"reloading data");
	[self.tableView reloadData];
}

- (void)viewDidAppear:(BOOL)animated {
	[super viewDidAppear:animated];
}

- (void)viewWillDisappear:(BOOL)animated {
}

- (void)viewDidDisappear:(BOOL)animated {
}

- (void)didReceiveMemoryWarning {
	[super didReceiveMemoryWarning];
}

#pragma mark Actions
-(void)backToPeopleList {
	NSLog(@"Returning to previous list");
	[self.navigationController popViewControllerAnimated:YES];
	NSLog(@"View Popped");
}
#pragma mark Person picker delegates
- (BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker 
	  shouldContinueAfterSelectingPerson:(ABRecordRef)person {
	NSMutableDictionary *personInfo = [DataManager sharedDataManager].currentPerson;
	NSLog(@"Selected Record ID");
	ABRecordID r  = ABRecordGetRecordID(person);
//	NSInteger i = r;
	//NSString *sr = [[NSString alloc] initWithFormat:@"%u",r];
	[personInfo setObject:[NSNumber numberWithInt:r] forKey:@"actorabref"];
	[self.tableView reloadData];
	[self dismissModalViewControllerAnimated:YES];
	return NO;
}
- (BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker 
		shouldContinueAfterSelectingPerson:(ABRecordRef)person 
								property:(ABPropertyID)property 
							  identifier:(ABMultiValueIdentifier)identifier {
	return NO;
}
- (void)peoplePickerNavigationControllerDidCancel:(ABPeoplePickerNavigationController *)peoplePicker {
//	[personInfo setObject:[NSNumber numberWithInt:] forKey:@"actorabref"];
	[self dismissModalViewControllerAnimated:YES];	 
}
@end

